/*
 * Copyright (c) 2011 Auzingers
 * Created by Oui-IT
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.auzingers.docsorganizer.gui;

import com.auzingers.docsorganizer.DocsOrganizerApplication;
import com.auzingers.docsorganizer.exception.DefaultExceptionHandler;

import android.app.Activity;
import android.os.Bundle;

/** Base class for all activities, contains some helper functions and a basic structure. */
public abstract class AbstractActivity extends Activity {

    private static final String TAG = DocsOrganizerApplication.APP_TAG + ".AbstractActivity";

    /**
     * Interface for executing tasks in an environment that handles errors that might
     * occur during processing - used together with executeSafely.
     */
    protected interface DOProcess {
        /**
         * The method that performs the actual processing.
         * @throws Exception if an error occurs.
         */
        public void run() throws Exception;
    }

    /**
     * Must be overwritten in subclasses to define the id of the layout that should be shown.
     * @return layout id for the current activity
     */
    protected abstract int getContentView();

    /** Retrieves the controls and stores them in member variables. Needs to set spinningWheel and refreshImage. */
    protected void getControls() {
        //can be overwritten in subclass
    }

    /**
     * Prepares the gui for display, i.e. makes it look better.
     */
    protected void setupGui() {
        //can be overwritten in subclass
    }

    /**
     *  Adds action listeners to gui controls.
     */
    protected void addActionListeners() {
        //can be overwritten in subclass
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentView());
        getControls();
        setupGui();
        addActionListeners();
    }

    /**
     * "Safely" executes the given action, that means: if an exception occurs, the exception is handled and
     * an error message is shown to the user. It is also logged and in case of an unexpected exception, it
     * is also send to ACRA.
     * @param process the process to execute
     */
    protected void executeSafely(DOProcess process) {
        try {
            process.run();
        } catch (Throwable ex) {
            DefaultExceptionHandler.handleGuiException(this, TAG, ex);
        }
    }
}
